package com.yangye.otherdemo.printoddeven;

import java.util.concurrent.locks.LockSupport;

/**
 * @author yangye
 * @date 2020/08/18
 */
public class LockSupportDemo {

	private static int count = 0;

	private static int limit = 10;

	static Thread t1,t2 = null;

	public static void main(String[] args) {
		t1 = new Thread(() -> {
			while (count < limit) {
				System.out.println(String.format("线程%s打印%d", Thread.currentThread().getName(), count++));
				LockSupport.unpark(t2);
				LockSupport.park();
			}
		});
		t2 = new Thread(() -> {
			while (count < limit) {
				LockSupport.park();
				System.out.println(String.format("线程%s打印%d", Thread.currentThread().getName(), count++));
				LockSupport.unpark(t1);
			}
		});
		t1.start();
		t2.start();
	}
}
